/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.mer.entity.WeddingProcess;
import com.kaaction.modules.mer.service.WeddingProcessService;

/**
 * 婚礼流程Controller
 * @author huanghaiming
 * @version 2017-06-02
 */
@Controller
@RequestMapping(value = "${adminPath}/mer/weddingProcess")
public class WeddingProcessController extends BaseController {

	@Autowired
	private WeddingProcessService weddingProcessService;
	
	@ModelAttribute
	public WeddingProcess get(@RequestParam(required=false) String id) {
		WeddingProcess entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = weddingProcessService.get(id);
		}
		if (entity == null){
			entity = new WeddingProcess();
		}
		return entity;
	}
	
	@RequiresPermissions("mer:weddingProcess:view")
	@RequestMapping(value = {"list", ""})
	public String list(WeddingProcess weddingProcess, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WeddingProcess> page = weddingProcessService.findPage(new Page<WeddingProcess>(request, response), weddingProcess); 
		model.addAttribute("page", page);
		return "modules/mer/weddingProcessList";
	}

	@RequiresPermissions("mer:weddingProcess:view")
	@RequestMapping(value = "form")
	public String form(WeddingProcess weddingProcess, Model model) {
		model.addAttribute("weddingProcess", weddingProcess);
		return "modules/mer/weddingProcessForm";
	}

	@RequiresPermissions("mer:weddingProcess:edit")
	@RequestMapping(value = "save")
	public String save(WeddingProcess weddingProcess, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, weddingProcess)){
			return form(weddingProcess, model);
		}
		weddingProcessService.save(weddingProcess);
		addMessage(redirectAttributes, "保存婚礼流程成功");
		return "redirect:"+Global.getAdminPath()+"/mer/weddingProcess/?repage";
	}
	
	@RequiresPermissions("mer:weddingProcess:edit")
	@RequestMapping(value = "delete")
	public String delete(WeddingProcess weddingProcess, RedirectAttributes redirectAttributes) {
		weddingProcessService.delete(weddingProcess);
		addMessage(redirectAttributes, "删除婚礼流程成功");
		return "redirect:"+Global.getAdminPath()+"/mer/weddingProcess/?repage";
	}

}